package com.lite.haru.alarmpolice.fragment;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.KJFragment;

import com.alibaba.fastjson.JSON;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.lite.haru.R;
import com.lite.haru.alarmpolice.activity.AlarmInfoDetailAty;
import com.lite.haru.alarmpolice.activity.AlarmMainAty;
import com.lite.haru.constant.Constant;
import com.lite.haru.entity.AlarmInfo;
import com.lite.haru.myadapter.MyAlarmInfoPendingAdp;
import com.lite.haru.tools.CheckNetWork;
import com.lite.haru.view.LoadListView;
import com.lite.haru.view.LoadListView.ILoadListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class AlarmPendingFrag extends KJFragment implements
		SwipeRefreshLayout.OnRefreshListener, ILoadListener {
	private KJHttp kjh;
	private int pageNow = 1;// 当前页
	private int pageSize = Constant.pageSize;//
	private List<AlarmInfo> pending_datas;// 待处理的数据
	private List<AlarmInfo> pending_datas_tmp;// 待处理的数据
	private MyAlarmInfoPendingAdp pendingAdapter;// 待处理信息的适配器
	@BindView(id = R.id.network)
	private TextView network;
	@BindView(id = R.id.data_empty)
	private TextView data_empty;
	@BindView(id = R.id.progressBar)
	private ProgressBar progressBar;
	@BindView(id = R.id.id_swipe_ly)
	private SwipeRefreshLayout mSwipeRefreshLayout;
	@BindView(id = R.id.pending_listview)
	private LoadListView pending_listview;

	@Override
	protected View inflaterView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
		View view = View.inflate(getActivity(),
				R.layout.frag_alarm_info_pending, null);
		return view;
	}

	@Override
	protected void initData() {
		kjh = new KJHttp();
		pending_datas = new ArrayList<AlarmInfo>();
		super.initData();
	}

	@Override
	protected void initWidget(View parentView) {
		pending_listview.setInterface(this);
		mSwipeRefreshLayout.setOnRefreshListener(this);
		mSwipeRefreshLayout.setColorSchemeResources(
				R.color.swiperefresh_color1, R.color.swiperefresh_color2,
				R.color.swiperefresh_color3, R.color.swiperefresh_color4);
		getPendingData();
		OnItemClick();
		super.initWidget(parentView);
	}

	public void getPendingData() {
		if (CheckNetWork.isNetworkAvailable(getActivity())) {
			mySetVisible(pending_listview);
			mySetVisible(data_empty);
			mySetGone(network);
			String url = Constant.url + "/AlarmInfoAction";
			HttpParams params = new HttpParams();
			params.put("pageNow", pageNow);
			params.put("pageSize", pageSize);
			params.put("param0", "get_pending_alarm_info");

			kjh.post(url, params, false, new HttpCallBack() {

				@Override
				public void onPreStart() {
					super.onPreStart();
					// progressBar.setVisibility(View.VISIBLE);
				}

				@Override
				public void onSuccess(String t) {

					pending_datas = JSON.parseArray(t, AlarmInfo.class);
//					for (AlarmInfo a : pending_datas_tmp) {
//						if (a.getCountry().equals("中国")
//								&& a.getProvince().equals("江苏省")
//								&& a.getCity().equals("盐城市")
//								&& a.getDistrict().equals("盐都区"))
//							pending_datas.add(a);
//					}
					if (pending_datas.isEmpty()) {
						data_empty.setVisibility(View.VISIBLE);
					} else {
						data_empty.setVisibility(View.GONE);
						pendingAdapter = new MyAlarmInfoPendingAdp(
								pending_listview, pending_datas,
								R.layout.item_alarm_info);
						pending_listview.setAdapter(pendingAdapter);
					}

					super.onSuccess(t);
				}

				@Override
				public void onFailure(int errorNo, String strMsg) {
					super.onFailure(errorNo, strMsg);

				}

				@Override
				public void onFinish() {
					super.onFinish();
				}
			});
		} else {
			mySetGone(pending_listview);
			mySetGone(data_empty);
			mySetVisible(network);
		}
	}

	@Override
	public void onRefresh() {
		// 设置顶部正在刷新
		setSwipeRefreshLoadingState();
		mHandler.sendEmptyMessageDelayed(Constant.REFRESH_COMPLETE, 2000);
		setSwipeRefreshLoadedState();
	}

	// 完成刷新
	protected void executeOnLoadFinish() {
		setSwipeRefreshLoadedState();
	}

	/** 设置顶部正在加载的状态 */
	protected void setSwipeRefreshLoadingState() {
		if (mSwipeRefreshLayout != null) {
			mSwipeRefreshLayout.setRefreshing(true);
			// 防止多次重复刷新
			mSwipeRefreshLayout.setEnabled(false);
		}
	}

	/** 设置顶部加载完毕的状态 */
	protected void setSwipeRefreshLoadedState() {
		if (mSwipeRefreshLayout != null) {
			mSwipeRefreshLayout.setRefreshing(false);
			mSwipeRefreshLayout.setEnabled(true);
		}
	}

	/**
	 * 显示控件
	 * 
	 * @param v
	 */
	public void mySetVisible(View v) {
		v.setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏控件
	 * 
	 * @param v
	 */
	public void mySetGone(View v) {
		v.setVisibility(View.GONE);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.REFRESH_COMPLETE:
				pageNow = 1;

				getPendingData();
				if (!pending_datas.isEmpty()) {
					mySetGone(data_empty);
					pendingAdapter.onDateChange(pending_datas);
				}

				mSwipeRefreshLayout.setRefreshing(false);
				break;
			}
		};
	};

	// 列表子选项的点击事件
	public void OnItemClick() {
		pending_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				AlarmInfo a = pending_datas.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt("id", a.getId());
				bundle.putString("username", a.getUsername());
				bundle.putString("phone", a.getPhone());
				bundle.putString("address", a.getAddress());
				bundle.putString("contactperson", a.getContactperson());
				bundle.putString("contactpersonphone",
						a.getContactpersonphone());
				bundle.putString("alarmtype", a.getAlarmtype());
				bundle.putString("alarmwork", a.getAlarmwork());
				bundle.putString("disaster", a.getDisaster());
				bundle.putString("grade", a.getGrade());
				bundle.putDouble("latitude", a.getLatitude());
				bundle.putDouble("longitude", a.getLongitude());
				bundle.putString("formatted_address", a.getFormatted_address());
				bundle.putString("sematic_description",
						a.getSematic_description());
				bundle.putString("flag", a.getFlag());
				Intent intent = new Intent(getActivity(),
						AlarmInfoDetailAty.class);
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});
	}

	@Override
	public void onLoad() {
		// TODO Auto-generated method stub

	}
}
